import Vue from 'vue'

import 'normalize.css/normalize.css'// A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import locale from 'element-ui/lib/locale/lang/zh-CN' // lang i18n

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'

import '@/icons' // icon
import '@/permission' // permission control
import 'babel-polyfill'
import { setToken } from '@/utils/auth'
// 引入中英文切换插件vue-i18n
import VueI18n from 'vue-i18n'
import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
// Vue.use(ElementUI, { locale })
import * as filters from './filters'
Vue.use(VueI18n) // 挂载
Vue.config.productionTip = false
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

const i18n = new VueI18n({
  // locale: 'zh-CN',    // 语言标识
  locale: localStorage.getItem('language') ? localStorage.getItem('language') : 'zh-CN',
  messages: {
    'zh-CN': {
      // 通过require引入中文语言包
      ...require('./lang/zh'),
      ...zhLocale
    },
    'en-US': {
      // 通过require引入英文语言包
      ...require('./lang/en'),
      ...enLocale
    }
  }
})

Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value)
})

router.beforeEach((to, from, next) => {
  // // NProgress.start();
  // if (to.path == '/login') {
  //   sessionStorage.removeItem('userInfo')
  // }
  const query = location.search.substring(1)
  console.log('登录信息' + query)
  if (query) {
    const token = query.split('=')
    sessionStorage.setItem('tokenSign', token[1])
    setToken(token[1])
    // if (store.getters.token) {
    //   next()
    // }
  }
  next()
})

new Vue({
  el: '#app',
  router,
  store,
  i18n,
  render: h => h(App)
})
